#ifndef PAGEUSERMGMT_H
#define PAGEUSERMGMT_H

#include <QWidget>
#include "core/databasepool/databasepool.h"
#include "core/threadpool/threadpool.h"
#include "modules/user-management/usermanagement.h"
#include "modules/device-management/devicemanagement.h"
#include "modules/data-monitor/datamonitor.h"
#include "modules/alert-manager/alertmanager.h"
#include "modules/data-analysis/dataanalysis.h"
#include "LogManager.h"
#include "mainbar.h"
#include <QSqlTableModel>
#include <QTableView>
#include <QSqlTableModel>
#include <QVBoxLayout>
#include <QSortFilterProxyModel>
#include <QStyledItemDelegate>
#include <QDateTime>
#include <QMutex>

//禁用编辑
class ReadOnlyDelegate : public QStyledItemDelegate {
public:
    explicit ReadOnlyDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {
        Q_UNUSED(parent); // 显式标记参数未使用
    }
    QWidget* createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const override {
        return nullptr; // 禁用编辑
    }
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override {
        QStyleOptionViewItem opt = option;
        opt.displayAlignment = Qt::AlignCenter;
        QStyledItemDelegate::paint(painter, opt, index);
    }
};

// 自定义委托用于设置文字居中
class CenteredItemDelegate : public QStyledItemDelegate {
public:
    explicit CenteredItemDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {}
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override {
        QStyleOptionViewItem opt = option;
        opt.displayAlignment = Qt::AlignCenter;
        QStyledItemDelegate::paint(painter, opt, index);
    }
};

// 时间格式化委托类
class DateTimeDelegate : public QStyledItemDelegate {
public:
    explicit DateTimeDelegate(QObject *parent = nullptr)
        : QStyledItemDelegate(parent) {}

    QString displayText(const QVariant &value, const QLocale &) const override {
        return QDateTime::fromString(value.toString(), "yyyy-MM-dd hh:mm:ss")
                .toString("yyyy-MM-dd HH:mm");
    }
};

namespace Ui {
class pageUserMgmt;
}

class pageUserMgmt : public QWidget
{
    Q_OBJECT

public:
    explicit pageUserMgmt(Mainbar *mainbar,QWidget *parent = nullptr);
    ~pageUserMgmt();

    // 添加访问器函数
    static QString getOriginalOldUsername();
    static QString getTempusername();

private slots:
    void receiveUsername(const QString &);
    void onSearch(const QString &text);      // 搜索用户
    void onDeleteUser();                     // 删除用户
    void onSaveChanges();                     // 保存修改
    void refreshTable();                      // 刷新表格

private:
    Ui::pageUserMgmt *ui;
    QSqlTableModel *m_model;
    UserManagement m_userMgr;
    QSortFilterProxyModel *m_proxyModel;      // 用于搜索过滤
    // 互斥锁，用于线程安全
    QMutex m_mutex;
    LogManager* m_logManager;
    Mainbar *m_mainbar;
    QMap<QString, QVariant> userInfo;
    QMap<QString, QVariant> userInfoLogin;
    static QString originalOldUsername;
    static QString Tempusername;
    static QString deletedUsers; // 静态变量用于记录删除的用户
    static QString loginUsername;
};

#endif // PAGEUSERMGMT_H
